package com.efounder.frame.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;

/**
 * Created by Administrator on 2016/11/24 0024.
 */

public class AppInfoUtils {
    Context context;
    PackageManager pm;
    Drawable appIcon = null;
    String appVersion = "";
    String appName = "";
    String appSignature = "";
    String[] appPremission = null;

    public AppInfoUtils(Context context) {
        this.context = context.getApplicationContext();
        pm = this.context.getPackageManager();
    }

    /*
     * 获取程序 图标
     */
    public Drawable getAppIcon(String packname) {
        try {
            ApplicationInfo info = pm.getApplicationInfo(packname, 0);
            appIcon = info.loadIcon(pm);
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
        return appIcon;
    }

    /*
     *获取程序的版本号
     */
    public String getAppVersion(String packname) {

        try {
            PackageInfo packinfo = pm.getPackageInfo(packname, 0);
            appVersion = packinfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();

        }
        return appVersion;
    }


    /*
     * 获取程序的名字
     */
    public String getAppName(String packname) {
        try {
            ApplicationInfo info = pm.getApplicationInfo(packname, 0);
            appName = info.loadLabel(pm).toString();
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
        return appName;
    }

    /*
     * 获取程序的权限
     */
    public String[] getAppPremission(String packname) {
        try {
            PackageInfo packinfo = pm.getPackageInfo(packname, PackageManager.GET_PERMISSIONS);
            //获取到所有的权限
            appPremission = packinfo.requestedPermissions;

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();

        }
        return appPremission;
    }


    /*
     * 获取程序的签名
     */
    public String getAppSignature(String packname) {
        try {
            PackageInfo packinfo = pm.getPackageInfo(packname, PackageManager.GET_SIGNATURES);
            //获取到所有的权限
            appSignature = packinfo.signatures[0].toCharsString();

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();

        }
        return appSignature;
    }
}